package com.cloudm.technician.ui.base

import android.annotation.SuppressLint
import android.support.multidex.MultiDexApplication
import com.blankj.utilcode.util.Utils
import com.cloudm.technician.BuildConfig
import com.cloudm.technician.di.*
import com.cloudm.technician.ui.service.InstallTreasureIntentService
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.FormatStrategy
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy
import com.umeng.analytics.MobclickAgent
import io.realm.Realm

/**
 * 作者: wesley
 * 时间: 2017/11/20
 * 描述: 程序的入口
 */
@SuppressLint("Registered") open class BaseApplication : MultiDexApplication() {

    val appComponent: AppComponent by lazy {
        initAppComponent()
    }

    override fun onCreate() {
        super.onCreate()
        initRealm()
        initLogger()
        initUmeng()
        initUtilCode()
        //initIntentService()
    }

    private fun initIntentService() {
        InstallTreasureIntentService.start(this)
    }

    private fun initRealm() {
        Realm.init(this)
    }

    private fun initLogger() {
        val formatStrategy: FormatStrategy = PrettyFormatStrategy.newBuilder().tag(
                packageName).methodCount(3).build()

        Logger.addLogAdapter(object : AndroidLogAdapter(formatStrategy) {
            override fun isLoggable(priority: Int, tag: String?): Boolean = BuildConfig.DEBUG
        })
    }


    private fun initUmeng(){
        MobclickAgent.setDebugMode(BuildConfig.DEBUG)
    }

    private fun initUtilCode(){
        Utils.init(this)
    }


    private fun initAppComponent(): AppComponent = DaggerAppComponent.builder().appModule(
            AppModule(this)).apiModule(ApiModule()).networkModule(
            NetworkModule(this)).dbPreferencesModule(DbPreferencesModule(this)).build()
}